<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Up
{
    protected $order;
    protected $set;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 报单区
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            $this->upPrice();
            $this->upChain();
        }

    }

    public function upPrice()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        if ($amount >= $this->set->vip2) {
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        }

        if ($amount >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }
    }

    protected function X($user_id, $first_box = 0, $team_box = 0, $level_count = 0, $leve = [])
    {
        if ($first_box > 0) {
            $first_count = Chain::getChildrenLevel($user_id, [
                UserLevel::$keys['vip1'],
                UserLevel::$keys['vip2'],
                UserLevel::$keys['vip3'],
                UserLevel::$keys['vip4']
            ]);
            if (count($first_count) < $first_box) {
                return false;
            }
        }

        if ($team_box > 0) {
            $ids = Chain::getChildrenAllQuick($user_id, [
                UserLevel::$keys['vip1'],
                UserLevel::$keys['vip2'],
                UserLevel::$keys['vip3'],
                UserLevel::$keys['vip4']
            ], ['id']);
            $ids = ArrayHelper::getColumn($ids, 'id');
            $box = Up::getBox($ids);
            if ($box < $team_box) {
                return false;
            }
        }

        if ($level_count > 0) {
            $count = Chain::getChildrenAllQuick($user_id, $leve, ['id']);
            if (count($count) < $level_count) {
                return false;
            }
        }

        return true;
    }

    public static function getBox($ids = [])
    {
        $order_ids = Order::find()
            ->where(['user_id' => $ids])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->select(['id'])
            ->column();
        $items = OrderItem::find()
            ->where(['order_id' => $order_ids])
            ->all();
        $sum = 0;
        foreach ($items as $item) {
            $count = (int)Product::find()
                ->where(['id' => $item['product_id']])
                ->select(['box_count'])
                ->scalar();
            $sum += $count * $item['quantity'];
        }

        return $sum;
    }

    public function upChain()
    {
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
        ], true);

        foreach ($chain as $user) {
            if ($user->level < UserLevel::$keys['vip1']) {
                continue;
            }

            if ($user->level < UserLevel::$keys['vip2']) {
                if ($this->X($user->id, $this->set->vip2_box_1, $this->set->vip2_box_team)) {
                    $user->level = UserLevel::$keys['vip2'];
                    $user->save();
                }
            }

            if ($user->level < UserLevel::$keys['vip3']) {
                if ($this->X($user->id, 0,
                    $this->set->vip3_box, $this->set->vip3_2,
                    [
                        UserLevel::$keys['vip2'],
                        UserLevel::$keys['vip3'],
                        UserLevel::$keys['vip4']
                    ])) {
                    $user->level = UserLevel::$keys['vip3'];
                    $user->save();
                }
            }

            if ($user->level < UserLevel::$keys['vip4']) {
                if ($this->X($user->id, 0,
                    $this->set->vip4_box, $this->set->vip4_2,
                    [
                        UserLevel::$keys['vip3'],
                        UserLevel::$keys['vip4']
                    ])) {
                    $user->level = UserLevel::$keys['vip4'];
                    $user->save();
                }
            }

            // 星级
            if ($user->level >= UserLevel::$keys['vip4']) {
                $this->_star($user);
            }
        }
    }

    private function _star(User $user)
    {
        if ($user->star < 5) {
            $this->_exe($user, 5, $this->set->up_5);
        }
        if ($user->star < 4) {
            $this->_exe($user, 4, $this->set->up_4);
        }
        if ($user->star < 3) {
            $this->_exe($user, 3, $this->set->up_3);
        }
        if ($user->star < 2) {
            $this->_exe($user, 2, $this->set->up_2);
        }
        if ($user->star < 1) {
            $this->_exe($user, 1, $this->set->up_1);
        }

    }

    private function _exe(User $user, $star, $need_count)
    {
        // 1) 找出直推线， 几条
        // 2) 找出线上是否存在CEO

        // $first = Chain::getChildrenAllQuick($user->id, [
        //     UserLevel::$keys['vip1'],
        //     UserLevel::$keys['vip2'],
        //     UserLevel::$keys['vip3'],
        //     UserLevel::$keys['vip4']
        // ], ['id']);
        //
        // $count = 0;
        // foreach ($first as $item) {
        //
        //     // 本身是不是CEO
        //     if ($item['level'] == UserLevel::$keys['vip4']) {
        //         $count++;
        //         continue;
        //     }
        //
        //     // 找出线上是否存在CEO
        //     $ceo = User::find()
        //         ->where(['referrer_id' => $item['id']])
        //         ->andWhere(['level' => UserLevel::$keys['vip4']])
        //         ->one();
        //     if ($ceo) {
        //         $count++;
        //         continue;
        //     }
        // }

        // 改成了直推多少个CEO
        $count = User::find()
            ->where(['referrer_id' => $user['id']])
            ->andWhere(['level' => UserLevel::$keys['vip4']])
            ->count();
        $count = intval($count);

        if ($count >= $need_count) {
            $user->star = $star;
            $user->save();
        }
    }

}